{extend name="public/world"}
{block name="style"}{/block}
{block name="container"}
    <div class="layui-side layui-side-menu" style="width:175px;background-color: #fff;margin: 10px 0 10px 10px;padding-top:20px;">
        <div id="typeTree"></div>
    </div>
    <div class="layui-fluid" style="margin-left: 180px;">
        <div class="layui-card">
            <div class="layui-card-body">
                <div class="layui-form" lay-filter="formSearch">
                    <div class="layui-field-box" style="padding:0;">
                        <div class="layui-form-item" style="margin-bottom:0">
                            <div class="layui-inline my-search">
                                <div class="layui-input-inline">
                                    <input type="text" name="name" value="" placeholder="按名称" autocomplete="off" class="layui-input" lay-filter="name">
                                </div>
                                <div class="layui-input-inline" style="width: 170px;">
                                    <input type="hidden" id="code" name="code" value="0">
                                    <button type="button" class="layui-btn layui-btn-radius layui-btn-primary layui-btn-sm" title="清除条件" style="padding: 0 8px" lay-submit lay-filter="reset"><i class="layui-icon" style="color:#009688;">&#xe640;</i></button>
                                    <button type="button" class="layui-btn layui-btn-radius layui-btn-sm" lay-submit lay-filter="search">搜索</button>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
                <table class="layui-hide" id="dataTable" lay-filter="dataTable"></table>
            </div>
        </div>
    </div>
{/block}
{block name="script"}
<script type="text/html" id="barDemo">
    {if isset($sonAuth['edit'])}
    <a href="{:url('/erp.Basics/edit')}?id={{ d.id }}" class="layui-btn layui-btn-xs j-iframe-pop" lxb-data="{width: '660px', height: '550px'}" title="修改商品">修改</a>
    {/if}
</script>
<script type="text/html" id="toolbar">
    <div class="layui-btn-group fl">
        {if isset($sonAuth['add'])}
        <a href="{:url('/erp.Basics/add')}" class="layui-btn layui-btn-xs layuiadmin-btn-admin j-iframe-pop" lxb-data="{width: '660px', height: '550px'}" title="添加商品">添加商品</a>
        {/if}
        {if isset($sonAuth['types'])}
        <button class="layui-btn layui-btn-xs layui-btn-normal"  lay-event="addType">分类管理</button>
        {/if}
    </div>
</script>
<script>
    layui.use(['form','table','tree'], function() {
        var $ = layui.$
            , form = layui.form
            , table = layui.table
            , tree = layui.tree;
        //渲染
        tree.render({
            elem: '#typeTree'  //绑定元素
            ,id: 'treeID'
            ,data: {:json_encode($treeData)}
            ,onlyIconControl: true  //是否仅允许节点左侧图标控制展开收缩
            ,showLine: true
            ,click: function(obj){
                $('.layui-tree-set').removeClass('layui-tree-show');
                if(obj.data.id > 0) {
                    $(obj.elem).addClass('layui-tree-show');
                    $('#code').val(obj.data.id);
                } else {
                    $('#code').val(0);
                }
                tableIns.reload({
                    where: form.val("formSearch"),
                    page: {curr: 1}
                });
            }
        });
        table.on('toolbar(dataTable)', function(obj){
            var them = window.top !== window.self ? window.top : window.self;
            if (obj.event == 'addType') {
                them.layer.open({
                    type: 2
                    ,title: '商品分类管理'
                    ,content: "{:url('/erp.Basics/types')}"
                    ,maxmin: true
                    ,area: ['80%', '95%']
                    ,btn: ['关闭']
                    ,btnAlign: 'c' //按钮居中
                    ,fixed: false //不固定
                    ,moveOut: true //true  可以拖出窗外  false 只能在窗内拖
                    ,anim: 5 //出场动画 isOutAnim bool 关闭动画
                    ,offset: 'auto' //['100px','100px'],//'auto',//初始位置  ['100px','100px'] t[ 上 左]
                    ,shade: .7 //遮罩
                    ,resize: true //是否允许拉伸
                    ,move: '.layui-layer-title' // 默认".layui-layer-title",// 触发拖动的元素

                    ,success: function(layero){
                        layero.find('.layui-layer-min').remove();
                        layero.find('.layui-layer-close2').addClass('layui-layer-close1').removeClass('layui-layer-close2');
                    }
                    ,yes: function(){
                        window.location.reload();
                        them.layer.closeAll();
                    }
                    ,cancel: function(){
                        window.location.reload();
                        them.layer.closeAll();
                    }
                });
                return false;
            }
        });
        form.on('submit(search)', function(){
            tableIns.reload({
                where: form.val("formSearch"),
                page: {curr: 1}
            });
        });
        //头工具栏事件
        form.on('submit(reset)', function(){
            var searchs = form.val("formSearch");
            for (var i in searchs) {
                if(i!='code') searchs[i] = '';
            }
            form.val("formSearch", searchs);
            tableIns.reload({
                where: form.val("formSearch"),
                page: {curr: 1}
            });
        });
        var tableIns = table.render({
            elem: '#dataTable'
            ,url: "{:url('/erp.Basics/index')}" //数据接口
            ,where: form.val("formSearch")
            ,page: true //开启分页
            ,skin: 'row'
            ,even: true
            ,size: 'sm'
            ,limit: 20
            ,text: {
                none : '暂无商品'
            }
            ,toolbar: '#toolbar'
            ,defaultToolbar: false
            ,cols: [[ //表头
                {field: 'name', width: 160, title: '商品名称', fixed: 'left'}
                ,{field: 'norm', width: 100, title: '规格说明'}
                ,{field: 'status', width: 80, title: '状态',templet: function(d){
                    if(d.status) return '<div>正常</div>';
                    else return '<div style="color: #FF5722">停用</div>';
                }}
                ,{field: 'stock', width: 80, title: '实际库存'}
                ,{field: 'start_stock', width: 100, title: '初始库存'}
                ,{field: 'max_stock', width: 100, title: '最大库存量'}
                ,{field: 'min_stock', width: 100, title: '最小库存量'}
                ,{field: 'unit', width: 100, title: '采购单位'}
                ,{field: 'price', width: 100, title: '实际成本'}
                ,{field: 'bar_code', width: 180, title: '条形码'}
                ,{field: 'factory', width: 180, title: '生产厂家'}
                ,{fixed: 'right', width: 80, title: '操作', templet: '#barDemo'}
            ]]
        });
        table.on('row(dataTable)', function(obj){
            if($(obj.tr).hasClass('row-in')) {
                $(obj.tr).removeClass('row-in');
            } else {
                $(obj.tr).addClass('row-in').siblings('tr').removeClass('row-in');
            }
        });
    });
</script>
{/block}